package action;

import java.util.ArrayList;
import java.util.List;

import model.GroupInfo;
import model.PageBean;
import model.TableInfo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import common.SplitArray;

import util.ResponseUtil;

import dao.GroupInfoDao;

public class GroupInfoAction extends BaseAction {

	private static final long serialVersionUID = -3122631143548390762L;
	private String page;
	private String rows;
	private String keyword;
	private GroupInfo groupInfo;
	private List<TableInfo> tables;
	private int groupNum;
	
	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getRows() {
		return rows;
	}

	public void setRows(String rows) {
		this.rows = rows;
	}

	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	public GroupInfo getGroupInfo() {
		return groupInfo;
	}

	public void setGroupInfo(GroupInfo groupInfo) {
		this.groupInfo = groupInfo;
	}
	
	public List<TableInfo> getTables() {
		return tables;
	}

	public void setTables(List<TableInfo> tables) {
		this.tables = tables;
	}

	public int getGroupNum() {
		return groupNum;
	}

	public void setGroupNum(int groupNum) {
		this.groupNum = groupNum;
	}

	GroupInfoDao groupInfoDao = new GroupInfoDao();

	@Override
	public String execute() throws Exception {
		PageBean pageBean = new PageBean(page == null? 0 : Integer.parseInt(page),
				rows == null ? 0 : Integer.parseInt(rows));
		JSONObject result = new JSONObject();
		try {
			JSONArray jsonArray = groupInfoDao
					.findAllGroupInfo(keyword, pageBean);
			for (int i = 0; i<jsonArray.size();i++) {
				JSONObject jo = jsonArray.getJSONObject(i);
				if(jo.containsKey("OWNER") && jo.containsKey("GROUP_NAME")) {
					String owner = jo.getString("OWNER");
					String groupName = jo.getString("GROUP_NAME");
					JSONArray tables = groupInfoDao.getTablesByOwnerGroup(request.getSession(), owner, groupName, pageBean);
					jo.put("GROUP_SOURCE", tables);
				}
			}
			int total = groupInfoDao.getGroupInfoCount(keyword);
			result.put("rows", jsonArray);
			result.put("total", total);
			result.put("success", "true");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", e.getMessage());
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}

	public String delete() throws Exception {
		JSONObject result = new JSONObject();
		try {
			int delNums = groupInfoDao.deleteGroupInfo(groupInfo.getOwner(), groupInfo.getGroupName());
			if (delNums > 0) {
				result.put("success", "true");
				result.put("delNums", delNums);
			} else {
				result.put("success", "false");
				result.put("errorMsg", "删除失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", e.getMessage());
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}

	public String save() throws Exception {
		JSONObject result = new JSONObject();
		try {
			groupInfoDao.deleteGroupInfo(groupInfo.getOwner(), groupInfo.getGroupName());;
			int saveNums = groupInfoDao.addGroupInfo(groupInfo);
			if (saveNums > 0) {
				result.put("success", "true");
			} else {
				result.put("success", "false");
				result.put("errorMsg", "保存失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", e.getMessage());
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}
	
	public String appendTables() throws Exception {
		JSONObject result = new JSONObject();
		try {
			int saveNums = groupInfoDao.addGroupInfo(groupInfo);
			if (saveNums > 0) {
				result.put("success", "true");
			} else {
				result.put("success", "false");
				result.put("errorMsg", "保存失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", e.getMessage());
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}
	
//	public String findTablesByOwnerGroup() throws Exception {
//		PageBean pageBean = new PageBean(page == null? 0 : Integer.parseInt(page),
//				rows == null ? 0 : Integer.parseInt(rows));
//		JSONObject result = new JSONObject();
//		try {
//			JSONArray jsonArray = groupInfoDao
//					.getTablesByOwnerGroup(request.getSession(), groupInfo.getOwner(), groupInfo.getGroupName(), pageBean);
//			int total = groupInfoDao.getTablesCountByOwnerGroup(request.getSession(), groupInfo.getOwner(), groupInfo.getGroupName());
//			result.put("rows", jsonArray);
//			result.put("total", total);
//			result.put("success", "true");
//		} catch (Exception e) {
//			e.printStackTrace();
//			result.put("success", "false");
//			result.put("errorMsg", e.getMessage());
//		}
//		ResponseUtil.write(ServletActionContext.getResponse(), result);
//		return null;
//	}
	
	public String groupTables() throws Exception {
		List<TableInfo> sources = new ArrayList<TableInfo>(tables);
		TableInfo temp = new TableInfo(null,null,"0","0");
		sources.add(0, temp);//To fit bag algorithm method
		SplitArray spliter = new SplitArray().execute(sources, groupNum);
		List<List<TableInfo>> groups = spliter.getResult();
		JSONObject result = new JSONObject();
		try {
			groupInfoDao.grouTables(groups);
			result.put("success", "true");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("success", "false");
			result.put("errorMsg", "保存失败");
		}
		ResponseUtil.write(ServletActionContext.getResponse(), result);
		return null;
	}

}
